from fastapi import APIRouter, HTTPException
from datetime import datetime
from api.models.schemas import HealthResponse, ApiResponse
from database import get_db_client
from utils.logger import get_logger

# 获取日志记录器
logger = get_logger(__name__)

router = APIRouter()


@router.get("/health", response_model=HealthResponse, summary="健康检查")
async def health_check():
    """
    健康检查接口
    
    检查服务状态和数据库连接
    """
    try:
        # 测试数据库连接
        db_client = get_db_client()
        db_connected = db_client.test_connection()
        
        status = "healthy" if db_connected else "degraded"
        
        response = HealthResponse(
            status=status,
            timestamp=datetime.now(),
            database_connected=db_connected
        )
        
        logger.info(f"🏥 健康检查: {status}, 数据库连接: {db_connected}")
        
        return response
        
    except Exception as e:
        logger.error(f"❌ 健康检查失败: {e}")
        raise HTTPException(status_code=500, detail="健康检查失败")


@router.get("/version", response_model=ApiResponse, summary="版本信息")
async def get_version():
    """获取API版本信息"""
    return ApiResponse(
        code=200,
        message="版本信息获取成功",
        data={
            "version": "1.0.0",
            "name": "牛马工时日志汇报系统 API",
            "description": "基于 AI 驱动的工时日志自动总结汇报系统"
        }
    ) 